/*****************************************************************
**                                                             	**
**  Copyright (C) 2004 Amlogic,Inc.                            	**
**  All rights reserved                                        	**
**        Filename : LoadingWin.c /Project:AVOS		       **
**        Revision : 1.1                                       	**
**                                                             	**
*****************************************************************/
#include "../menu.h"
#include "LoadingWinEvent.h"

INT32S LoadingWinMsgProc(PHWND hwnd, INT32U msg_type, INT32S lpParam)
{
    switch( msg_type){
        case WM_SHOWWINDOW:
            return LoadingWinOnShow(hwnd, lpParam) ;
        case WM_HIDEWINDOW:
            return LoadingWinOnHide(hwnd, lpParam) ;
    }
    return 0 ;
}

void SetLoadingWinPalette(void)
{
    YUVENTRY mask_para[4] = {0,0,0,0xfff};
    AWSetAlphaMask(0,4, mask_para);
    YUVENTRY palette_para[16] = {0x8000 | 16392,0x8000 | 21176,0x8000 | 17752,0x8000 | 16392,17592,19496,19832,19656,19992,18104,17673,17817,15864,15721,15912,16344};
    AWSetPalette(0,16, palette_para);
    return ;
}

void SetLoadingWinPos(HWND* pControl, INT8U CurOsdSize)
{
    switch(CurOsdSize){
		case OsdSize576i:
	 	case OsdSize576p:
	 	case OsdSize576iCVBS:
            RECT WindowRect576 = {264,446,183,45};
            pControl->winrect = WindowRect576;
            CREATEWINDOWPARAM WindowPara576 = {1,{{264,446,183,45},    {0,0,0,0},{0,0,0,0},{0,0,0,0}},{PF_PALETTE_256,0,0,0},    SetLoadingWinPalette};
            AWSendMessage(pControl, WM_WINDOW_RESET_DRAW_AREA, (INT32S)&WindowRect576);
            break ;
			
        case OsdSize480i:
	 	case OsdSize480p:
	 	case OsdSize480iCVBS:
            RECT NormalWindowRect = {264,398,183,45};
            pControl->winrect = NormalWindowRect;
            CREATEWINDOWPARAM NormalWindowPara = {1,{{264,398,183,45},    {0,0,0,0},{0,0,0,0},{0,0,0,0}},{PF_PALETTE_256,0,0,0},    SetLoadingWinPalette};
            AWSendMessage(pControl, WM_WINDOW_RESET_DRAW_AREA, (INT32S)&NormalWindowPara);
            break ;

    	    #ifdef SUPPORT_VGA
		case OsdSizeVga:
            RECT VgaWindowRect = {264+WinStart_x,398+WinStart_y,183,45};
            pControl->winrect = VgaWindowRect;
            CREATEWINDOWPARAM VgalWindowPara = {1,{{264+WinStart_x,398+WinStart_y,183,45},    {0,0,0,0},{0,0,0,0},{0,0,0,0}},{PF_PALETTE_256,0,0,0},    SetLoadingWinPalette};
            AWSendMessage(pControl, WM_WINDOW_RESET_DRAW_AREA, (INT32S)&VgalWindowPara);
            break ;
    	    #endif
			
    }//end of switch
    return;
}

HWND *CreateControlLoadingWin(HWND* pParent,INT8U CurOsdSize)
{
    HWND* pNewControl;
    switch(CurOsdSize){
		case OsdSize576i:
	 	case OsdSize576p:
	 	case OsdSize576iCVBS:
            CREATEWINDOWPARAM WindowPara576 = {1,{{264,446,183,45},{0,0,0,0},{0,0,0,0},{0,0,0,0}},{PF_PALETTE_256,0,0,0},SetLoadingWinPalette};
            //Create Menu Window -- 1
            pNewControl = AWCreateWinControl("TWindow","LoadingWin",WS_OVERLAPPEDWINDOW,
		                      264,446,183,45,NULL, (INT32S)&WindowPara576,LoadingWinMsgProc) ;
            pNewControl->win_id = 0 ;
            break ;
		
    	case OsdSize480i:
	 	case OsdSize480p:
	 	case OsdSize480iCVBS:
            CREATEWINDOWPARAM Normalwindow_para = {1,{{264,398,183,45},{0,0,0,0},{0,0,0,0},{0,0,0,0}},{PF_PALETTE_256,0,0,0},SetLoadingWinPalette};
            //Create Menu Window -- 1
            pNewControl = AWCreateWinControl("TWindow","LoadingWin",WS_OVERLAPPEDWINDOW,
		                      264,398,183,45,NULL, (INT32S)&Normalwindow_para,LoadingWinMsgProc) ;
            pNewControl->win_id = 0 ;
            break ;

    	    #ifdef SUPPORT_VGA
		case OsdSizeVga:
            CREATEWINDOWPARAM Vgawindow_para = {1,{{264+WinStart_x,398+WinStart_y,183,45},{0,0,0,0},{0,0,0,0},{0,0,0,0}},{PF_PALETTE_256,0,0,0},SetLoadingWinPalette};
            //Create Menu Window -- 1
            pNewControl = AWCreateWinControl("TWindow","LoadingWin",WS_OVERLAPPEDWINDOW,
		                      264+WinStart_x,398+WinStart_y,183,45,NULL, (INT32S)&Vgawindow_para,LoadingWinMsgProc) ;
            pNewControl->win_id = 0 ;
            break ;		
    	    #endif			
    }//end of switch
            return pNewControl;
}

void SetLoadingPos(HWND* pControl, INT8U CurOsdSize)
{
    int iLoop;
    switch(CurOsdSize){
	#ifdef SUPPORT_VGA
	 case OsdSizeVga:
            pControl->winrect.top = pControl->parent->winrect.top + 0-WinStart_y;
            pControl->winrect.left = pControl->parent->winrect.left + 0-WinStart_x;
            pControl->winrect.width = 183;
            pControl->winrect.height = 45;
            pControl->draw_info_num = 1;
            RECT VgaDrawRect[1] = {
            {pControl->winrect.left + 0,pControl->winrect.top + 0,183,45},
            };
            for(iLoop=0; iLoop < 1; iLoop++)
                memcpy(&(pControl->draw_info[iLoop]), &VgaDrawRect[iLoop], sizeof(RECT));
            break ;
	#endif
			
        default:
            pControl->winrect.top = pControl->parent->winrect.top + 0;
            pControl->winrect.left = pControl->parent->winrect.left + 0;
            pControl->winrect.width = 183;
            pControl->winrect.height = 45;
            pControl->draw_info_num = 1;
            RECT NormalDrawRect[1] = {
            {pControl->winrect.left + 0,pControl->winrect.top + 0,183,45},
            };
            for(iLoop=0; iLoop < 1; iLoop++)
                memcpy(&(pControl->draw_info[iLoop]), &NormalDrawRect[iLoop], sizeof(RECT));
            break ;
    }//end of switch
    return;
}

INT32S LoadingMsgProc(PHWND hwnd, INT32U msg_type, INT32S lpParam)
{
    switch( msg_type){
        case WM_PAINT:
            return LoadingOnPaint(hwnd, lpParam) ;
        default:
            return LoadingOnOtherEvent(hwnd, msg_type, lpParam) ;
    }
    return 0 ;
}

HWND *CreateControlLoading(HWND *pParent,INT8U CurOsdSize)
{
    //Create Control -- Loading
    HWND *pNewControl;
    switch(CurOsdSize){
	#ifdef SUPPORT_VGA
	 case OsdSizeVga:
            pNewControl = AWCreateWinControl("TControl","Loading",(WS_CHILDWINDOW | WS_CHILDWINDOW),
		                      0-WinStart_x,0-WinStart_y,183,45,pParent,0,LoadingMsgProc) ;
            pNewControl->win_id = 1 ;
            pNewControl->draw_info_num = 1;
            pNewControl->draw_info = (PDRAWINFO)AVMem_umalloc(sizeof(DRAWINFO) * pNewControl->draw_info_num) ;
            DRAWINFO VgaDrawDatas[1] = {
                          {0,0,0,0,(INT32U)LOADING_STR,DT_RESOURCE_STRING_DATA}};
            memcpy(pNewControl->draw_info,VgaDrawDatas, sizeof(DRAWINFO) * pNewControl->draw_info_num) ;
            break ;
	#endif
		
        default:
            pNewControl = AWCreateWinControl("TControl","Loading",(WS_CHILDWINDOW | WS_CHILDWINDOW),
		                      0,0,183,45,pParent,0,LoadingMsgProc) ;
            pNewControl->win_id = 1 ;
            pNewControl->draw_info_num = 1;
            pNewControl->draw_info = (PDRAWINFO)AVMem_umalloc(sizeof(DRAWINFO) * pNewControl->draw_info_num) ;
            DRAWINFO NormalDrawDatas[1] = {
                          {0,0,0,0,(INT32U)LOADING_STR,DT_RESOURCE_STRING_DATA}};
            memcpy(pNewControl->draw_info,NormalDrawDatas, sizeof(DRAWINFO) * pNewControl->draw_info_num) ;
            break ;
    }//end of switch
            return pNewControl;
}

HWND* CreateMenuLoadingWin(void)
{
    HWND *pNewControl ;
    TWINDOWS *pWin;
    HWND *pLoadingWin = CreateControlLoadingWin(NULL,CurrentOsdSize);
    SetLoadingWinPos(pLoadingWin,CurrentOsdSize);

    pNewControl= CreateControlLoading(pLoadingWin,CurrentOsdSize);
    SetLoadingPos(pNewControl,CurrentOsdSize);

    pWin = (TWINDOWS *)pLoadingWin;
    pWin->pFocusControl = AWFindWinCtrlByName("Loading");
    return pLoadingWin;
}

void WinResetLoadingWinControlsPos(HWND *pControl, INT8U CurOsdSize)
{
    switch(pControl->win_id){
        case 0://LoadingWin
            SetLoadingWinPos(pControl,CurOsdSize) ;
            break ;
        case 1://Loading
            SetLoadingPos(pControl,CurOsdSize) ;
            break ;
    }//end of switch
}
void WinResetLoadingWinPos(HWND *pControl, INT8U CurOsdSize)
{
    HWND *pChildControl;
    if(pControl == NULL)
        pControl = AWFindWinCtrlByName("LoadingWin");
    if(pControl == NULL) return ;

    WinResetLoadingWinControlsPos(pControl, CurOsdSize) ;
    pChildControl = pControl->children ;
    while(pChildControl != NULL){
    	WinResetLoadingWinPos(pChildControl, CurOsdSize) ;
    	pChildControl = pChildControl->siblings ;
	}
	return ;
}

